package com.example.demo.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.custom.BaseController;
import com.example.demo.entity.Position;
import com.example.demo.entity.Position;
import com.example.demo.entity.SysDept;
import com.example.demo.form.PositionForm;
import com.example.demo.form.PositionForm;
import com.example.demo.service.PositionService;
import com.example.demo.service.SysDeptService;

@RestController
@RequestMapping(value = "/position")
public class PositionController extends BaseController<Position,Integer, PositionForm> {
	@Autowired
	private PositionService service;
	@Autowired
	private SysDeptService deptservice;

	@SuppressWarnings("serial")
	@Override
	public Specification<Position> buildSpec(PositionForm form) {
		Specification<Position> spec=new Specification<Position>() {
			@Override
			public Predicate toPredicate(Root<Position> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String code = form.getDeptName();
				String name = form.getName();
				if(StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {
					HashSet<Predicate> rules2=new HashSet<>();
					rules2.add(cb.like(root.get("name"), "%"+search+"%"));
					rules.add(cb.or(rules2.toArray(new Predicate[rules2.size()])));
				}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	 @Override
	    public Object save(PositionForm form) {
	        try {
	            Position model = new Position();
	            Integer id = form.getId();
	            if (id != null) {
	                model = service.findById(id);
	            }
	            Integer deptId = form.getDeptId();
	            if (deptId != null) {
	                model.setDept(new SysDept(deptId));
	            }
	            BeanUtils.copyProperties(form, model, "id", "dept");
	            service.save(model);
	            return new AjaxResult("数据保存成功");
	        } catch (Exception e) {
	            return new AjaxResult(false, "数据保存失败");
	        }
	    }
	 
	 @RequestMapping(value = "/findByDept")
	 public Object findBydept(PositionForm form) {
		 Integer id=form.getDeptId();
		 SysDept dept=deptservice.findById(id);
		 return new AjaxResult(service.findByDept(dept));
	 }

}
